// global property definitions
var MapTag = 0;
var MapObjectName;

// Starts
function Start () 
{ 
	// just for debug
	CreateMap("penglai103");
	
	//anim = Resources.LoadAll("map/nspt/00000166");//动画图片的文件夹.放一起
	//mFrameCount = anim.Length;
	//print("xxx"+mFrameCount);

	animationPlane = new GameObject ("Plane");	
	animationPlane.transform.localScale = Vector3(400,200,-1);
	animationPlane.transform.localRotation = Quaternion.identity;
	animationPlane.transform.localPosition = Vector3(400, -1700, 0);
	
	var mesh : MeshFilter;
	mesh = animationPlane.AddComponent(MeshFilter);	
	mesh.mesh=Resources.Load("Plane-Z", Mesh);
	
	var redert : MeshRenderer;
	redert = animationPlane.AddComponent(MeshRenderer);
}

// create map by map name
function CreateMap(mapName)
{
	MapObjectName =	"Map"+ (++MapTag);
	
	var Map : GameObject;
	Map = new GameObject (MapObjectName);

	// loads nmap file
	var nmapTxt : TextAsset;
    nmapTxt = Resources.Load("map/nmap/" + mapName, typeof(TextAsset)) as TextAsset;		
    
    // Debug.Log(nmapTxt.text);
    
    // parses lines from nmap file
	var lineArray : String[];
	lineArray = nmapTxt.text.Split("\n".ToCharArray());
	
	// iterator all lines
   	for(var i = lineArray.length - 1; i>=0; i--)
   	{
	   if (lineArray[i] != "")
	   {
	   		var itemNo : String;
	   		itemNo = lineArray[i].Split(",".ToCharArray())[0];
	   		
			// Debug.Log(itemNo);
	    	
	    	// ensures length of item no as 8
	    	var mapTitleName= itemNo;
	 		var mapTitleNameLen = mapTitleName.length;
		 	for (var j = 0; j < 8 - mapTitleNameLen; j++)
		 	{
		 		mapTitleName = "0" + mapTitleName;
		 	}
        	
        	var itemX : float;
        	var itemY : float;
        	
        	itemX = float.Parse(lineArray[i].Split(",".ToCharArray())[2]);
        	itemY = float.Parse(lineArray[i].Split(",".ToCharArray())[3]);
        	
        	// create map title
			CreateMapTitle(mapTitleName, itemX, itemY, i); 
		}
	}
}

// creates map title
function CreateMapTitle(textureName, x, y, index)
{
	var texture : Texture;
	texture = Resources.Load("map/nspt/" + textureName);
	
	if (texture != null)
	{
		var Plane : GameObject;
		Plane = new GameObject (textureName);
		Plane.transform.parent =GameObject.Find(MapObjectName).transform;
		Plane.transform.localScale = Vector3(texture.width,texture.height,1);
		Plane.transform.localRotation = Quaternion.identity;
		Plane.transform.localPosition = Vector3(x + texture.width/2, - y - texture.height/2, -index);
		Plane.layer = 2;

		var mesh : MeshFilter;
		mesh = Plane.AddComponent(MeshFilter);
		mesh.mesh = Resources.Load("Plane-Z", Mesh);

		var redert : MeshRenderer;
		redert = Plane.AddComponent(MeshRenderer);

		Plane.renderer.material.mainTexture=texture ;
		Plane.renderer.material.shader = Shader.Find("Unlit/Transparent");
		
		//Plane.renderer.depth=i+2.0;
		//Plane.depth=i+2.0;
		//print("ssssss"+Plane.depth);
	}
	else
	{
		Debug.Log(textureName + " cannot be found...");
	}
}

// init, navigation creation for testing purpose
function OnGUI()
{
 	if(GUI.Button(new Rect(50,150,250,100),"ChangAn Map"))
    {
        Destroy(GameObject.Find(MapObjectName));
        CreateMap("changan_stage001(1)");
    }

    if(GUI.Button(new Rect(50,250,250,100),"ZhuDong Map"))
    {
        Destroy(GameObject.Find(MapObjectName));
        CreateMap("penglai103");
    }
    
    if(GUI.Button(new Rect(50,350,250,100),"ZhuDong Map - Test"))
    {
        Destroy(GameObject.Find(MapObjectName));
        CreateMap("penglai1031");
    }
    
    if(GUI.Button(new Rect(50,450,250,100),"Haidi"))
    {
        Destroy(GameObject.Find(MapObjectName));
        CreateMap("haidi109");
    }    
}

// updates
function Update() 
{
//     var index : int = (Time.time * 10) % anim.Length;
//     animationPlane.renderer.material.mainTexture = anim[index]; 

	if(Input.GetKey(KeyCode.LeftArrow)) 
	{
		camera.transform.position.x = camera.transform.position.x - 10;
	}
	
	if(Input.GetKey(KeyCode.RightArrow)) 
	{
		camera.transform.position.x = camera.transform.position.x + 10;
	}
    
    if(Input.GetKey(KeyCode.UpArrow)) 
    {
    	camera.transform.position.y = camera.transform.position.y + 10;
    }
    
    if(Input.GetKey(KeyCode.DownArrow)) 
    {
    	camera.transform.position.y = camera.transform.position.y - 10;
    }
}

